﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace popoBCadd_in
{
    class OFB
    {
        static int blocksize;
        static Queue<byte> q;

        private static byte[] getblock()
        {
            byte[] hasil = new byte[q.Count];
            for (int i = 0; i < q.Count; i++)
            {
                hasil[i] = q.ElementAt(i);
            }
            return hasil;
        }

        public static byte[] encrypt(byte[] message, byte[] key, byte[] iv)
        //enkripsi CFB 8-bit
        {
            blocksize = key.Length;
            q = new Queue<byte>();
            byte[] x;
            byte[] k;
            byte[] hasil = new byte[message.Length];

            //isi antrian dengan initial vector
            for (int i = 0; i < iv.Length; i++)
            {
                q.Enqueue(iv[i]);
            }

            for (int i = 0; i < message.Length; i++)
            {
                x = getblock();
                k = feistel.feistelencrypt(x, key);
                hasil[i] = (byte)((int)k[1] ^ (int)message[i]);
                q.Enqueue(k[1]);
                q.Dequeue();
            }

            return hasil;
        }

        public static byte[] decrypt(byte[] message, byte[] key, byte[] iv)
        {
            blocksize = key.Length;
            q = new Queue<byte>();
            byte[] x;
            byte[] k;
            byte[] hasil = new byte[message.Length];

            //isi antrian dengan initial vector
            for (int i = 0; i < iv.Length; i++)
            {
                q.Enqueue(iv[i]);
            }

            for (int i = 0; i < message.Length; i++)
            {
                x = getblock();
                k = feistel.feistelencrypt(x, key);
                hasil[i] = (byte)((int)k[1] ^ (int)message[i]);
                q.Enqueue(k[1]);
                q.Dequeue();
            }

            return hasil;
        }
    }
}
